﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class CustomerJiage : BasePage
    {
        ExcuteDao dao = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["UrlType"] != null && Request.QueryString["UrlType"].Equals("menu"))
            {
                Session["SortExpression_chanpinjiageList"] = null;
                Session["SortDirection_chanpinjiageList"] = null;
                Session["txtCPCode"] = null;

            }

            if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                string pageIndex = Request.QueryString["pageIndex"];
                int index = Convert.ToInt32(pageIndex);
                gvchanpinjiage.PageIndex = index;

                Session["chanpinjiageListIndex"] = index;

                setSessionData();
            }
            //设置分页状态
            if (Session["SortExpression_chanpinjiageList"] != null)
            {
                this.gvchanpinjiage.Attributes["SortExpression_chanpinjiageList"] = Session["SortExpression_chanpinjiageList"].ToString();
                this.gvchanpinjiage.Attributes["SortDirection_chanpinjiageList"] = Session["SortDirection_chanpinjiageList"].ToString();
                setSortColumnStyle(this.gvchanpinjiage, Session["SortExpression_chanpinjiageList"].ToString(), Session["SortDirection_chanpinjiageList"].ToString());
            }
            //setSortColumnStyle(gvchanpinjiage, this.gvchanpinjiage.Attributes["SortExpression_chanpinjiageList"].ToString(),
            //     this.gvchanpinjiage.Attributes["SortDirection_chanpinjiageList"].ToString());
            if (!IsPostBack)
            {
                ViewState["chanpinjiageAll"] = dao.Get_CustomerChanPinJiage_ALL(SpecialCharHandler.HtmlEncode(txtCPCode.Text));

                SearchchanpinjiageBindData();
            }
        }

        private void setSessionData()
        {
            //检索条件记录
            if (Session["txtCPCode"] != null)
            {
                txtCPCode.Text = SpecialCharHandler.HtmlDiscode(Session["txtCPCode"].ToString());
            }
            if (Session["chanpinjiageListIndex"] != null)
            {
                gvchanpinjiage.PageIndex = (int)Session["chanpinjiageListIndex"];
            }
        }
        #region 信息列表事件

        /// <summary>
        /// 信息列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvchanpinjiage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");
                //((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/Editchanpinjiage.aspx?chanpinjiageID="
                //    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;") + "&mid=" + mid;

                //e.Row.Cells[1].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);

                //string jianjie = SpecialCharHandler.HtmlDiscode(e.Row.Cells[2].Text);
                //e.Row.Cells[2].ToolTip = jianjie;
                //if (jianjie.Length > 15)
                //{
                //    jianjie = jianjie.Substring(0, 15) + "...";
                //}
                //e.Row.Cells[2].Text = jianjie;
                //e.Row.Cells[3].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[3].Text);
                //e.Row.Cells[4].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[4].Text);

            }
        }
        #endregion

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["chanpinjiageAll"] = dao.Get_CustomerChanPinJiage_ALL(SpecialCharHandler.HtmlEncode(txtCPCode.Text));
            //清空分页状态
            Session["SortExpression_chanpinjiageList"] = null;
            Session["SortDirection_chanpinjiageList"] = null;

            //检索条件记录
            Session["txtCPCode"] = SpecialCharHandler.HtmlEncode(txtCPCode.Text);

            SearchchanpinjiageBindData();

        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvchanpinjiage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvchanpinjiage.PageIndex = e.NewPageIndex;
            pageIndex = e.NewPageIndex;
            Session["chanpinjiageListIndex"] = pageIndex;
            SearchchanpinjiageBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvchanpinjiage_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_chanpinjiageList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_chanpinjiageList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_chanpinjiageList == this.gvchanpinjiage.Attributes["SortExpression_chanpinjiageList"])
            {
                //获得下一次的排序状态
                SortDirection_chanpinjiageList = (this.gvchanpinjiage.Attributes["SortDirection_chanpinjiageList"].ToString() == SortDirection_chanpinjiageList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvchanpinjiage.Attributes["SortExpression_chanpinjiageList"] = SortExpression_chanpinjiageList;
            this.gvchanpinjiage.Attributes["SortDirection_chanpinjiageList"] = SortDirection_chanpinjiageList;
            //记录分页状态
            Session["SortExpression_chanpinjiageList"] = SortExpression_chanpinjiageList;
            Session["SortDirection_chanpinjiageList"] = SortDirection_chanpinjiageList;

            setSortColumnStyle(this.gvchanpinjiage, SortExpression_chanpinjiageList, SortDirection_chanpinjiageList);
            this.SearchchanpinjiageBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchchanpinjiageBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_chanpinjiageList = this.gvchanpinjiage.Attributes["SortExpression_chanpinjiageList"];
            string SortDirection_chanpinjiageList = this.gvchanpinjiage.Attributes["SortDirection_chanpinjiageList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["chanpinjiageAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_chanpinjiageList)) && (!string.IsNullOrEmpty(SortDirection_chanpinjiageList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_chanpinjiageList, SortDirection_chanpinjiageList);
            }
            // GridView绑定并显示数据
            this.gvchanpinjiage.DataSource = dtBind;
            this.gvchanpinjiage.DataBind();

            pageCount = gvchanpinjiage.PageCount;
            pageIndex = gvchanpinjiage.PageIndex;

        }

        protected void gvchanpinjiage_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvchanpinjiage.EditIndex = e.NewEditIndex;
            SearchchanpinjiageBindData();
        }

        protected void gvchanpinjiage_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ViewState["chanpinjiageAll"] = dao.Get_CustomerChanPinJiage_ALL(SpecialCharHandler.HtmlEncode(txtCPCode.Text));
            gvchanpinjiage.EditIndex = -1;
            SearchchanpinjiageBindData();
        }

        protected void gvchanpinjiage_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            GridViewRow row = gvchanpinjiage.Rows[e.RowIndex];
            double zuixinjiage = 0;
            try
            {
                zuixinjiage = Convert.ToDouble(((TextBox)row.Cells[3].Controls[0]).Text);
            }
            catch
            {
                ShowMsg("最新价格的格式不正确，请确认！", 0);
                return;
            }
            string cpCode = row.Cells[0].Text;
            string cpqixiri = row.Cells[1].Text.Replace("&nbsp;", "");
            string cpdaoqiri = row.Cells[2].Text.Replace("&nbsp;", "");

            //string aa4 = ((TextBox)row.Cells[3].Controls[0]).Text;

            gvchanpinjiage.EditIndex = -1;

            //dao.Update_BuyListALL
            //添加用户信息到数据库中
            bool isOk = false;
            if (dao.Insert_Jiage(cpCode, cpqixiri, cpdaoqiri, zuixinjiage.ToString(), this.getCurrentUser().UserName) == -1)
            {
                isOk = true;
            }

            if (isOk)
            {
                ShowMsg("设置产品最新价格成功！", 0);
            }
            else
            {
                ShowMsg("设置产品最新价格失败，请确认！", 1);
            }

            ViewState["chanpinjiageAll"] = dao.Get_CustomerChanPinJiage_ALL(SpecialCharHandler.HtmlEncode(txtCPCode.Text));
            gvchanpinjiage.EditIndex = -1;
            SearchchanpinjiageBindData();
        }

        protected void gvchanpinjiage_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow row = gvchanpinjiage.Rows[e.NewSelectedIndex];
            string cpCode = row.Cells[0].Text;
            string cpqixiri = row.Cells[1].Text.Replace("&nbsp;", "").TrimEnd();
            string cpdaoqiri = row.Cells[2].Text.Replace("&nbsp;", "").TrimEnd();

            Response.Redirect("CustomerJiageHis.aspx?code=" + cpCode + "&qixiri=" + cpqixiri + "&daoqiri=" + cpdaoqiri, true);
        }
    }
}